package com.lry.mqstarter.utils;

import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.Field;

public class MergePropertiesUtil {

    /**
     * 合并消费者配置，以properties为主，defaultProp为辅
     * @param properties
     * @param defaultProp
     * @return
     */
    @SneakyThrows
    public static void mergerProperties(Object properties, Object defaultProp){
        if(null==properties||null==defaultProp){
            return;
        }
        Field[] fields = properties.getClass().getDeclaredFields();

        for (Field field : fields) {
            field.setAccessible(true);
            Object obj = field.get(properties);

            Field defaultField = defaultProp.getClass().getDeclaredField(field.getName());
            defaultField.setAccessible(true);
            Object defaultObj = defaultField.get(defaultProp);
            if(null==obj){
                field.set(properties,defaultObj);
            }else{
                if(obj instanceof String){
                    if(StringUtils.isEmpty((String)obj)){
                        field.set(properties,defaultObj);
                    }
                }
            }
        }
    }


}
